package com.practice.exploreandroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        Button creditCheckButton;
        creditCheckButton = (Button)findViewById(R.id.creditCheckButton);
        
     //	Setup Spinner (drop down list)
        Spinner personSpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.personList, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        personSpinner.setAdapter(adapter);

        
     // build dialog box to display when button is clicked (denied)
        AlertDialog.Builder deniedBuilder = new AlertDialog.Builder(this);
        deniedBuilder.setMessage(R.string.denied)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                }
            });
        final AlertDialog deniedAlert = deniedBuilder.create();
        
        // build dialog box to display when button is clicked (accepted)
        AlertDialog.Builder acceptedBuilder = new AlertDialog.Builder(this);
        acceptedBuilder.setMessage(R.string.accepted)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                }
            });
        final AlertDialog acceptedAlert = acceptedBuilder.create();

        // click listener on the button
        creditCheckButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Spinner personSpinner = (Spinner) findViewById(R.id.spinner);
            	if (personSpinner.getSelectedItemPosition() == 0 || personSpinner.getSelectedItemPosition() == 2)
            		deniedAlert.show();
            	else
            		acceptedAlert.show();
            }
            });
        
    }
}